<?php


namespace app\common\controller;


use app\common\lib\RedisKey;
use app\common\lib\RespCode;
use custom\Redis;
use custom\Token;
use think\App;
use think\facade\Request;

class BadminController extends BaseController {

    protected $headersFields = ['adcode', 'token'];

    //门店不能访问的接口
    protected $banShop = [];

    public function initialize(App $app = null) {
        parent::initialize($app);
    }

    /**
     * 权限验证
     */
    protected function auth() {
        if (!in_array('*', $this->notNeedToken) && !in_array(strtolower($this->request->action()), $this->notNeedToken)) {
            if (empty($this->headers['token'])) {
                $this->resErr('请登录访问', RespCode::TOKEN_EMPTY);
            }
            $Token = new Token();
            if (!$this->uInfo = $Token->verifyToken($this->headers['token'], [], $Token::BADMIN_TOKEN, RedisKey::BADMIN_TOKEN_HASH)) {
                $this->resErr();
            }
            if ($this->uInfo['company_shop_id'] != 0 && (in_array(strtolower($this->request->action()), $this->banShop) || in_array('*', $this->banShop))) {
                $this->resErr('当前账号类型不是公司账号', RespCode::AUTH_ERR);
            }
            //验证重复提交
            if (in_array(strtolower($this->request->action()), $this->resubmitLock)) {
                if (Request::isPost()) {
                    $this->resubmitLock($this->uInfo['id'], strtolower($this->request->action()), 1);
                }
            }
        }
    }

    /**
     * 重复提交的锁
     * @param $uid
     * @param $action
     */
    protected function resubmitLock($uid, $action, $ex) {
        $key = $uid . "_{$action}";
        if (!$this->resubmitredis($key, $ex)) {
            $this->resErr('请求太频繁', RespCode::DEFAULT_ERR);
        }
    }

    public function resubmitredis($key, $expire) {
        $redisModel = Redis::getInstance();
        $key = "badmin_resubmitLock_$key";
        return $redisModel->set($key, 1, ['nx', 'ex' => $expire]);
    }

    /**
     * 参数处理
     * @param $fields
     * @return mixed
     * @throws \think\Exception
     */
    protected function checkParams($fields) {
        parent::checkParams($fields);
        if (!empty($this->uInfo)) {
            empty($this->params['user_id']) && $this->params['user_id'] = $this->uInfo['id'];//Company_account id
            empty($this->params['company_id']) && $this->params['company_id'] = $this->uInfo['company_id'];//Company_account company_id
            empty($this->params['company_shop_id']) && $this->params['company_shop_id'] = $this->uInfo['company_shop_id'];//Company_account company_shop_id
            empty($this->params['account_type']) && $this->params['account_type'] = empty($this->uInfo['company_shop_id']) ? 0 : 1;// 0：公司账号，1：门店账号
        }
        $adcode = $this->headers['adcode'];
        !empty($adcode) && empty($this->params['city_code']) && $this->params['city_code'] = $adcode;

        $controller = $this->request->controller();
        //访问房源相关的控制器判断请求城市
        if (strtolower("$controller") == "house") {
            $company_codes = db('company')->where('id', $this->params['company_id'])->value('city_code');
            $company_codes = explode(',', $company_codes);
            $arr = [];
            foreach ($company_codes as $company_code) $arr[] = explode('-', $company_code)[0];
            if (!in_array($this->params['city_code'], $arr)) $this->resErr('当前访问城市不存在，请刷新后尝试', RespCode::FORBIDDEN);
        }
        return $this->params;
    }
}

